﻿using System;
using Microsoft.Practices.Unity;
using Shop.Core.Domain.Catalog;
using Shop.Core.Domain.Common;
using Shop.Core.Events;
using Shop.Infrastructure.Containers;

namespace Shop.Core.Domain.Orders
{
    [Serializable]
    public partial class OrderProduct : BaseEntity<long>
    {
        private Product originalProduct = null;
        /// <summary>
        /// 原始商品
        /// </summary>
        public virtual Product OriginalProduct
        {
            get
            {
                if (this.originalProduct == null)
                {
                    this.originalProduct = Container.Current.Resolve<IEventPublisher>().EntityRequired<Product, string>(this.Sku);
                }
                return this.originalProduct;
            }
            set
            {
                this.originalProduct = value;
                this.Sku = this.originalProduct.ProductId;
                this.Price = this.originalProduct.Price;
                this.CostPrice = this.originalProduct.CostPrise;
            }
        }

        public virtual string Sku
        {
            get;
            protected set;
        }
        /// <summary>
        /// 订单号
        /// </summary>
        public virtual string OrderNumber
        {
            get;
            set;
        }
        /// <summary>
        /// 购买数量
        /// </summary>
        public virtual int Quantity
        {
            get;
            set;
        }
        /// <summary>
        /// 成本价
        /// </summary>
        public virtual Money CostPrice
        {
            get;
            protected set;
        }
        /// <summary>
        /// 销售单价。这个价格已经刨去优惠金额。
        /// </summary>
        public virtual Money Price
        {
            get;
            protected set;
        }
        /// <summary>
        /// 优惠金额
        /// </summary>
        public virtual Money Discount
        {
            get;
            set;
        }
        /// <summary>
        /// 总价
        /// </summary>
        public virtual Money TotalAmount
        {
            get
            {
                return this.Price * this.Quantity;
            }
        }
        /// <summary>
        /// 商品类型
        /// </summary>
        public virtual OrderProductType ProductType
        {
            get;
            set;
        }
        public virtual DateTime CreatedOn
        {
            get;
            set;
        }
        public virtual int CreatedBy
        {
            get;
            set;
        }

        public virtual bool IsDeleted
        {
            get;
            protected set;
        }
    }
}
